package com.panasonic.web.service.impl;


import com.panasonic.common.core.redis.RedisCache;
import com.panasonic.common.utils.DateUtils;
import com.panasonic.web.domain.cms.CmsConfig;
import com.panasonic.web.mapper.CmsConfigMapper;
import com.panasonic.web.service.ICmsConfigService;
import com.panasonic.web.util.CmsSecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 配置Service业务层处理
 *
 * @author lwf
 * @date 2021-11-12
 */
@Service
public class CmsConfigServiceImpl implements ICmsConfigService {

    private static final String CMS_CONFIG_KEY = "cms_config_settings";
    @Autowired
    private CmsConfigMapper cmsConfigMapper;


    @Autowired
    private RedisCache redisCache;

    /**
     * 查询配置
     *
     * @param id 配置ID
     * @return 配置
     */
    @Override
    public CmsConfig selectCmsConfigById(Long id) {
        return cmsConfigMapper.selectCmsConfigById(id);
    }

    /**
     * 查询配置列表
     *
     * @param cmsConfig 配置
     * @return 配置
     */
    @Override
    public List<CmsConfig> selectCmsConfigList(CmsConfig cmsConfig) {
        return cmsConfigMapper.selectCmsConfigList(cmsConfig);
    }

    /**
     * 新增配置
     *
     * @param cmsConfig 配置
     * @return 结果
     */
    @Override
    public int insertCmsConfig(CmsConfig cmsConfig) {
        return cmsConfigMapper.insertCmsConfig(cmsConfig);
    }

    /**
     * 修改配置
     *
     * @param cmsConfig 配置
     * @return 结果
     */
    @Override
    public int updateCmsConfig(CmsConfig cmsConfig) {
        cmsConfig.setUpdateTime(DateUtils.getNowDate());
        return cmsConfigMapper.updateCmsConfig(cmsConfig);
    }

    /**
     * 批量删除配置
     *
     * @param ids 需要删除的配置ID
     * @return 结果
     */
    @Override
    public int deleteCmsConfigByIds(Long[] ids) {
        return cmsConfigMapper.deleteCmsConfigByIds(ids);
    }

    /**
     * 删除配置信息
     *
     * @param id 配置ID
     * @return 结果
     */
    @Override
    public int deleteCmsConfigById(Long id) {
        return cmsConfigMapper.deleteCmsConfigById(id);
    }

    @Override
    public void save(CmsConfig config) {
        config.setUpdateBy(CmsSecurityUtils.getUsername());
        config.setUpdateTime(DateUtils.getNowDate());
        redisCache.setCacheObject(CMS_CONFIG_KEY, config);
        cmsConfigMapper.insertCmsConfig(config);
    }

    @Override
    public CmsConfig selectCmsConfig() {
        CmsConfig config = redisCache.getCacheObject(CMS_CONFIG_KEY);
        if (config == null) {
            config = cmsConfigMapper.selectCmsConfigByMaxId();
            redisCache.setCacheObject(CMS_CONFIG_KEY, config);
        }
        return config;
    }
}
